<template>
  <div id="goods_app">
    <div id="goods_control">
      <el-button type="danger" plain round @click="isShow.flag = true"
        >新增</el-button
      >
    </div>
    <el-table
      :data="goodsList"
      width=" 100%"
      default-expand-all
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID" fixed="left" width="100px">
      </el-table-column>
      <el-table-column prop="firstcatename" label="一级分类" width="180px">
      </el-table-column>
      <el-table-column prop="secondcatename" label="二级分类" width="180px">
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" width="180px">
      </el-table-column>
      <el-table-column prop="img" label="商品图片" width="180px">
        <template v-slot='hhh'>
        <img :src="$static_url+hhh.row.img" v-if="hhh.row.img" alt="" width="40%">
        </template>
      </el-table-column>
      <el-table-column prop="price" label="零售价格" width="180px">
      </el-table-column>
      <el-table-column prop="market_price" label="市场价格" width="180px">
      </el-table-column>
      <el-table-column prop="isnew" label="是否新品" width="180px">
        <template v-slot="hhh">
          <el-switch
            v-model="hhh.row.isnew"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="ishot" label="是否热卖" width="180px">
        <template v-slot="hhh">
          <el-switch
            v-model="hhh.row.ishot"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="180px">
        <template v-slot="hhh">
          <el-button
            type="success"
            size="mini"
            plain
            round
            v-if="hhh.row.status == 1"
            >正常</el-button
          >
          <el-button type="danger" size="mini" plain round v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" fixed="right" width="180px">
        <template v-slot="$props">
          <el-button
            type="warning"
            size="mini"
            plain
            round
            @click="edit_goods($props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            plain
            round
            @click="del_goods($props.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="pagintor.size"
      @current-change="take_page"
    >
    </el-pagination>
    <Dialog :isShow='isShow' @reload='reload' ref="dialog_ref"/>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import Dialog from '../components/AddGoods.vue'
export default {
  data() {
    return {
      isShow:{flag:false},
      pagintor: { page: 1, size: 5 },
    };
  },
  components:{
    Dialog
  },
  created() {
    this.goodsCountActions();
    this.goodsListActions(this.pagintor);
  },
  computed: {
    ...mapState("goods_model", ["goodsList", "count"]),
  },
  methods: {
    edit_goods(obj){
      this.$refs.dialog_ref.sendData(obj)
      this.isShow.flag = true
    },
    del_goods(id){
      this.$axios.post('/goodsdelete',{id}).then(res=>{
        if(res.code === 200){
          this.$message({
            type:'success',
            message:res.msg
          })
          this.reload()
        }
      })
    },
    reload(){
      this.goodsCountActions();
      this.goodsListActions(this.pagintor);
    },
    take_page(page) {
      this.pagintor.page = page;
      this.goodsCountActions();
      this.goodsListActions(this.pagintor);
    },
    ...mapActions("goods_model", ["goodsListActions", "goodsCountActions"]),
  },
};
</script>

<style>
#goods_control {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}

.el-table__header thead,
.el-table__header thead th {
  background-color: #ff674b;
  color: #fff;
  line-height: 40px;
  border: 0;
}
</style>